package com.cqc.paoclient.ui.code.codedetail;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cqc.paoclient.R;
import com.cqc.paoclient.base.BaseActivity;
import com.cqc.paoclient.model.BaseMsgModel;
import com.cqc.paoclient.model.CodeDetailModel;
import com.cqc.paoclient.util.GlideUtil;
import com.cqc.paoclient.view.HtmlTextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${cqc} on 2017/11/28.
 * 代码tab2 点击进入的详情
 */

public class CodeDetailActivity extends BaseActivity<CodeDetailPresenter> implements CodeDetailContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.iv_thumb)
    ImageView ivThumb;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_size)
    TextView tvSize;
    @BindView(R.id.tv_category)
    TextView tvCategory;
    @BindView(R.id.iv_collection)
    ImageView ivCollection;
    @BindView(R.id.tv_like_num)
    TextView tvLikeNum;
    @BindView(R.id.iv_comment)
    ImageView ivComment;
    @BindView(R.id.tv_comment_num)
    TextView tvCommentNum;
    @BindView(R.id.iv_go_github)
    ImageView ivGoGithub;
    @BindView(R.id.tv_click_num)
    TextView tvClickNum;
    @BindView(R.id.htv_code)
    HtmlTextView htvCode;
    private int codeId;
    private Intent intent;

    @Override
    public int getLayoutId() {
        return R.layout.activity_code_detail;
    }

    @Override
    public CodeDetailPresenter getmPresenter() {
        return new CodeDetailPresenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null) {
            mPresenter.setView(this);
        }
    }

    @Override
    public void initView() {
        intent = getIntent();
        toolbar.setTitle(intent.getStringExtra("title"));
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public void initData() {

        codeId = intent.getIntExtra("id", 0);

        mPresenter.getCodeDetail(codeId);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void getCodeDetailSuccess(CodeDetailModel codeDetailModel) {
        htvCode.setRichText(codeDetailModel.getReadme());
        initCodeDetail(codeDetailModel);
    }

    @Override
    public void showFail(String msg) {
        showShort(msg);
    }

    @Override
    public void stowSuccess(BaseMsgModel baseMsgModel) {
        showShort(baseMsgModel.getMessage());
    }

    /**
     * 初始化代码详情
     *
     * @param codeDetailModel 详情
     */
    private void initCodeDetail(CodeDetailModel codeDetailModel) {
        GlideUtil.loadDefaultImage(codeDetailModel.getThumbnail()).into(ivThumb);
        tvName.setText(codeDetailModel.getTitle());
        tvSize.setText(codeDetailModel.getSize());
        tvCategory.setText(codeDetailModel.getCodecategory().getCatename());
        tvLikeNum.setText(String.valueOf(codeDetailModel.getStow()));
        tvCommentNum.setText(String.valueOf(codeDetailModel.getComments()));
        tvClickNum.setText(String.valueOf(codeDetailModel.getClick()));
    }


    @OnClick({R.id.iv_collection, R.id.iv_comment, R.id.iv_go_github})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_collection:
                mPresenter.stow(codeId);
                break;
            case R.id.iv_comment:
                break;
            case R.id.iv_go_github:
                break;
        }
    }
}
